package com.tuanbiao.models.action;

import androidx.annotation.NonNull;

import com.abiao.lib_utils.Utils;
import com.alibaba.fastjson.JSONObject;
import com.hzjq.core.bean.BinVersion;
import com.hzjq.core.bean.Param;
import com.hzjq.data.base.CapWorkMode;
import com.hzjq.data.callback.OnResultProgressCallback;
import com.hzjq.data.entity.SettingParams;
import com.hzjq.lib.BlastKool;
import com.hzjq.lib.cap.bean.VersionResult;
import com.hzjq.lib.util.AssetsFileKool;
import com.tuanbiao.data.callback.OnBaseResultCallback;
import com.tuanbiao.data.callback.OnProgressCallback;

import java.io.File;

import io.reactivex.functions.Consumer;
import timber.log.Timber;

/**
 * @author abiao
 * @since 2022/4/25 14:20
 */
public interface SettingAction {
    default void setMaxDetonator(int maxDetonator) {
        BlastKool.Companion.loader().setMaxSupportPlanCapSize(maxDetonator);
    }

    default void setMaxDelay(long maxDelay) {
        BlastKool.Companion.loader().setMaxSupportCapDelay(maxDelay);
    }

    default long getMaxDelay() {
        return BlastKool.Companion.loader().getMaxSupportCapDelay();
    }

    default void setModel(boolean keep, boolean isStandAloneModel) {
        BlastKool.Companion.loader().enableStandModel(isStandAloneModel, keep);
    }


    default SettingParams getSettingParams() {
        return BlastKool.Companion.loader().quick().getSettings();
    }

    default void setHighModel(CapWorkMode workMode, OnBaseResultCallback<Boolean> callback) {
        BlastKool.Companion.loader().newSettingBuilder()
                .setWorkMode(workMode).build(aBoolean -> {
            callback.onSuccess(aBoolean);
            callback.onFinish();
        });
    }

    default void getParams(OnBaseResultCallback<Param> callback) {
        BlastKool.Companion.loader().param().getParam(param -> {
            Timber.i(param.toString());
            callback.onSuccess(param);
            callback.onFinish();
        });
    }

    default void updateSettingParam(Param param, OnBaseResultCallback<Boolean> callback) {
        Timber.i("updateSettingParam:"+JSONObject.toJSONString(param));
        BlastKool.Companion.loader().param().update(param, new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                Timber.i("update:"+JSONObject.toJSONString(param));
                callback.onSuccess(aBoolean);
                callback.onFinish();
            }
        });
    }

    default void upgrade(OnProgressCallback<Integer> callback) {
        AssetsFileKool.Companion.readAssetsBinFile(Utils.getApp(), new Consumer<File>() {
            @Override
            public void accept(File file) throws Exception {
                callback.onBefore();
                BlastKool.Companion.loader().upgrade().upgrade(31, file, new OnResultProgressCallback<BinVersion>() {
                    @Override
                    public void onResult(BinVersion v) {
                        callback.onSuccess(v.getBinVersion());
                        callback.onAfter();
                        Timber.i("onResult#" + v.getBinVersion());
                    }

                    @Override
                    public void onProgressChanged(@NonNull String s, int i, int i1) {
                        callback.onProgress(i, s);
                        Timber.i("onProgressChanged#" + s + ",i:" + i + ",i1:" + i1);
                    }

                    @Override
                    public void onFailed(@NonNull String s) {
                        callback.onFailed(s);
                        callback.onAfter();
                        Timber.i("onFailed#" + s);
                    }
                });
            }
        });
    }

    default void getAppVersion(OnBaseResultCallback<VersionResult> callback) {
        BlastKool.Companion.loader().version().getVersion(versionResult -> {
            callback.onSuccess(versionResult);
            callback.onFinish();
        });
    }

    default void savePseudoLocation(boolean use, String latitude, String longitude, OnBaseResultCallback<Boolean> callback) {
        BlastKool.Companion.loader().newSettingBuilder().enablePseudoLocation(use)
                .setPseudoLocation(Float.parseFloat(latitude), Float.parseFloat(longitude))
                .build(aBoolean -> {
                    callback.onSuccess(aBoolean);
                    callback.onFinish();

                });

    }

    default void setCapCountNotMatchAllowAdopt(boolean isAllowAdopt) {
        BlastKool.Companion.loader().setCapCountNotMatchAllowAdopt(isAllowAdopt);
    }

    default boolean isCapCountNotMatchAllowAdopt() {
        return BlastKool.Companion.loader().isCapCountNotMatchAllowAdopt();
    }

    default void changeForceBlast(boolean check){
        BlastKool.Companion.loader().setForceBlastCheck(check);
    }

    default void changeForceAuth(boolean check){
        BlastKool.Companion.loader().setForceAuthCheck(check);
    }

    default boolean isForceBlastCheck(){
        return BlastKool.Companion.loader().isForceBlastCheck();
    }
}
